
window.requestAnimFrame = (function() {
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
            return window.setTimeout(callback, 1000 / 60);
        };
})();
class Sprite {
    constructor(source) {
        this.image = {};
        this.currentImage = 0;
        this.movement = false;
        this.color = '';
        this.x = 0;
        this.y = 0;
        this.angle = 0;
        this.rotation = 0;
        this.alive = true;
        this.inWorld = true;
        this.a = 0;
        this.gravity = 0;
        this.speedX = 0;
        this.speedY = 0;
        Object.assign(this, source);
        this.w = this.w ||this.image.width;
        this.h = this.h || this.image.height;
    }
}
class Game {
    constructor(width, height, element, object) {
        this.sprite = [];
        this.building = [];
        this.width = element.width = width;
        this.height = element.height = height;
        this.ctx = element.getContext("2d");
        this.preload = object.preload;
        this.create = object.create;
        this.update = object.update;
        this.render = object.render;
        this.score = 0;
        this.state = {
            start : false,
            pause : false,
            end : false,
        };
        this.gamePlay = false;
        this.gameOver = false;
        this.load = {
            image: (name, src, num = 1) => {
                if(!(name in this)) this[name] = [];
                let onlyOne = (num == 1);
                for(let i = 0; i < num; i++){
                    this.imageLoaded = false;
                    let newImg = new Image;
                    newImg.onload = () => this.imageLoaded = true;
                    newImg.src = onlyOne ? src : src.replace(/ID/, i);
                    console.log("加载完成：" + name);
                    this[name].push(newImg);
                }
                return this.load;
            }
        };
        this.add = {
            sprite: (source) => {
                let newSprite = new Sprite(source);
                this.sprite.push(newSprite);
                return newSprite;
            },
            building: (source) => {
                let newSprite = new Sprite(source);
                this.building.push(newSprite);
                return newSprite;
            }
        }
    }

    gameLoop(){
        let gameLoop = () => {
            count++;
            this.T = requestAnimFrame(gameLoop);
            if(!this.gameOver){
                this.update();
                this.render();
                this.ctx.clearRect(0, 0, this.width, this.height);
                this.drawText();
                for(let i = 0; i < this.sprite.length; i++){
                    let CS = this.sprite[i];
                    CS.speedX += CS.a;
                    CS.speedY += CS.gravity;
                    CS.x += CS.speedX;
                    CS.y += CS.speedY;
                    this.ctx.drawImage(CS.image[CS.currentImage], CS.x, CS.y, CS.w, CS.h);
                    if(CS.movement && count%3 == 0){
                        CS.currentImage++;
                        if(CS.currentImage >= CS.image.length){
                            CS.currentImage = 0;
                        }
                    }
                }
                for(let i = 0; i < this.building.length; i++){
                    let currentBuild = this.building[i];
                    currentBuild.x += currentBuild.speedX;
                    currentBuild.y += currentBuild.speedY;
                    currentBuild.draw(this.ctx);
                }
            }else{
                //hint.style.display = "block";
            }
        };
        gameLoop();
    }
    drawText() {
        this.ctx.save();
        //分数显示
        this.ctx.fillStyle = "#ffffff";
        this.ctx.font = "24px YouYuan";
        this.ctx.fillText("分数:" + this.score, 1000, 50);
        this.ctx.font = "18px YouYuan";
        this.ctx.fillStyle = "#cccccc";
        this.ctx.fillText("玩法介绍:", 1000, 100);
        //玩法介绍
        if(!this.gamePlay){
            this.ctx.fillText("我才不得告诉你→_→", 1000, 130);
            this.ctx.fillText("除非……", 1000, 160);
            this.ctx.fillText("你死了一百次以上", 1000, 190);
            this.ctx.fillText("或者……", 1000, 220);
            this.ctx.fillText("承认你很弱！嘻嘻嘻嘻", 1000, 250);
        }else{
            this.ctx.fillText("-------Y(^_^)Y-------", 1000, 130);
            this.ctx.fillText("切换颜色：空格键", 1000, 160);
            this.ctx.fillText("跳    跃：Enter键", 1000, 190);
            this.ctx.fillText("人物颜色必须和桥的颜色", 1000, 220);
            this.ctx.fillText("相同！", 1000, 250);
            this.ctx.fillText("人在空中不能跳跃！", 1000, 280);
            this.ctx.fillText("出来就死不是BUG！", 1000, 310);
            this.ctx.fillText("是在考验你的人品……", 1000, 340);
            this.ctx.fillText("-------- ^_^ --------", 1000, 370);
        }
        //提示游戏结束
        if(this.gameOver) {
            this.alpha += count*0.0005;
            if(this.alpha>1) this.alpha=1;
            this.ctx.font = "34px YouYuan";
            this.ctx.fillStyle = "#ffffff";
            this.ctx.shadowBlur = 10;
            this.ctx.shadowColor = "white";
            this.ctx.fillStyle = "rgba(255,255,255," + this.alpha+")";
            this.ctx.fillText("GAME OVER!", this.width*0.5 - 80, this.height*0.4);
        }
        this.ctx.restore();
    }
    init() {
        this.preload();
        this.create();
    }
    start() {
        this.gameLoop();
    }
    restart(){
        hint.style.display = "none";
        this.score = 0;
        this.sprite = [];
        this.buliding = [];
        this.create();
        this.gameOver = false;
    }
    stop(){
        this.gameOver = true;
    }
}
export {Game}
var count = 0;

